<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>outcome&lt;&gt; - Boost.Outcome documentation</title>
<link rel="stylesheet" href="../../css/boost.css" type="text/css">
<meta name="generator" content="Hugo 0.52 with Boostdoc theme">
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>

<link rel="icon" href="../../images/favicon.ico" type="image/ico"/>
<body><div class="spirit-nav">
<a accesskey="p" href="../../tutorial/essential/result/try_ref.html"><img src="../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../tutorial/essential.html"><img src="../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../tutorial/essential/outcome/inspecting.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content">

  <div class="titlepage"><div><div><h1 style="clear: both">outcome&lt;&gt;</h1></div></div></div>


<p>Type <a href="../../reference/aliases/outcome.html" class="api-reference"><code>outcome&lt;T, EC = varies, EP = varies, NoValuePolicy = policy::default_policy&lt;T, EC, EP&gt;&gt;</code></a>
 represents either a successfully computed value of type <code>T</code>, or one or two reasons for failure. Failure can be represented by <code>EC</code>, or <code>EP</code>, or both, although usually it will either be an <code>EC</code> or an <code>EP</code>.
Similarly to <code>result</code>, <code>EC</code> defaults to <code>std::error_code</code>/<code>boost::system::error_code</code>, and <code>EP</code> defaults to <code>std::exception_ptr</code>/<code>boost::exception_ptr</code>.</p>

<p>The distinction is made into two types, <code>EC</code> and <code>EP</code>:</p>

<ul>
<li><code>EC</code> represents a <em>recoverable</em> failure from a lower-layer function, perhaps which was returned through <a href="../../reference/aliases/result.html" class="api-reference"><code>result&lt;T, E = varies, NoValuePolicy = policy::default_policy&lt;T, E, void&gt;&gt;</code></a>
.</li>
<li><code>EP</code> represents an <em>unrecoverable</em> failure where a C++ exception would ordinarily have been thrown. This is usually a pointer to an exception throw.</li>
</ul>

<p>It should be emphasised that this distinction is by convention only, but it will be confusing to your
users if you deviate significantly from this convention.</p>

<hr>

<h3 id="legacy-codebases">Legacy codebases</h3>

<p><code>outcome</code> is useful for transporting exceptions across layers of the program that were never designed with exception safety in mind.</p>

<p>Consider a program consisting of three layers:</p>


<center><img src="../../tutorial/essential/outcome/layer_chart.gif"></center>

<p>The highest-level layer, <code>Layer3</code>, uses exceptions for signalling failures. The middle layer, <code>Layer2_old</code>,
was not designed with exception safety in mind and functions need to return information about failures in return value.
But down in the implementation details, in <code>Layer1</code>, another library is used that again throws exceptions. The goal is
to be able to transfer an exception thrown in <code>Layer1</code> through <code>Layer2_old</code>, which is not exception-safe,
and be able to rethrow it in <code>Layer3</code>.</p>

<p>In <code>Layer1</code> we have two functions from two libraries: one reports failures by throwing exceptions, the other by returning <code>result&lt;&gt;</code>:</p>

<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="k">auto</span> <span class="nf">f</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="kt">int</span><span class="p">;</span>  <span class="c1">// throws on failure
</span><span class="c1"></span><span class="k">auto</span> <span class="nf">g</span><span class="p">()</span> <span class="k">noexcept</span> <span class="o">-&gt;</span> <span class="n">outcome</span><span class="o">::</span><span class="n">result</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="p">;</span>
</code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/using_outcome.cpp#L36" class="code-snippet-url" target="_blank">View this code on Github</a></div>


<p>In <code>Layer2_old</code> we cannot use exceptions, so its function <code>h</code> uses return type <code>outcome&lt;&gt;</code> to report failures. It is using functions <code>f</code> and <code>g</code> and reports their failures inside <code>outcome&lt;&gt;</code>:</p>

<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="k">auto</span> <span class="n">old</span><span class="o">::</span><span class="n">h</span><span class="p">()</span> <span class="k">noexcept</span> <span class="o">-&gt;</span> <span class="n">outcome</span><span class="o">::</span><span class="n">outcome</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span>
<span class="p">{</span>
  <span class="n">BOOST_OUTCOME_TRY</span><span class="p">(</span><span class="k">auto</span> <span class="n">i</span><span class="p">,</span> <span class="p">(</span><span class="n">g</span><span class="p">()));</span>             <span class="c1">// #1
</span><span class="c1"></span>    
  <span class="k">try</span> <span class="p">{</span>
    <span class="k">return</span> <span class="n">i</span> <span class="o">+</span> <span class="n">f</span><span class="p">();</span>
  <span class="p">}</span>
  <span class="k">catch</span> <span class="p">(...)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="n">std</span><span class="o">::</span><span class="n">current_exception</span><span class="p">();</span> <span class="c1">// #2
</span><span class="c1"></span>  <span class="p">}</span>
<span class="p">}</span>
</code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/using_outcome.cpp#L56" class="code-snippet-url" target="_blank">View this code on Github</a></div>


<p>#1. Operator <code>TRY</code> can forward failures encoded in <code>result&lt;T, EC&gt;</code> as <code>outcome&lt;T, EC, EP&gt;</code> without any loss in information. You can also use <code>TRY</code> to forward failure from one <code>outcome&lt;&gt;</code> to another.</p>

<p>#2. You can store the current exception through <code>std::exception_ptr</code> inside <code>outcome&lt;T, EC, EP&gt;</code> without any loss in information
    (provided that <code>EP</code> is <code>std::exception_ptr</code>).</p>



        </div><p><small>Last revised: March 19, 2019 at 22:57:48 &#43;0100</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../tutorial/essential/result/try_ref.html"><img src="../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../tutorial/essential.html"><img src="../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../tutorial/essential/outcome/inspecting.html"><img src="../../images/next.png" alt="Next"></a></div></body>
</html>
